package cn.jbt.api.admin.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "sys_user", schema = "jbt")
public class SysUser {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @NotBlank(message = "登陆账号不能为空")
    @Pattern(regexp = "^[a-zA-Z]\\w{4,19}$", message = "登陆账号格式错误")
    @Column(name = "username", nullable = false, length = 64)
    private String username;

    @Column(name = "password", nullable = false, length = 2000)
    private String password = "";

    @Column(name = "salt", nullable = false, length = 16)
    private String salt = "";

    @Pattern(regexp = "^$|^1[3456789][0-9]{9}$", message = "手机号码格式错误")
    @Column(name = "mobile", nullable = false, length = 11)
    private String mobile = "";

    @NotBlank(message = "用户昵称不能为空")
    @Column(name = "nickname", nullable = false, length = 200)
    private String nickname;

    @Column(name = "work", nullable = false, length = 32)
    private String work = "";

    @Column(name = "avatar", nullable = false, length = 200)
    private String avatar = "";

    @NotNull(message = "性别不能为空")
    @Min(value = 0, message = "性别'值'错误")
    @Max(value = 2, message = "性别'值'错误")
    @Column(name = "sex", nullable = false)
    private Integer sex;

    @Pattern(regexp = "^$|^\\w+((-\\w+)|(\\.\\w+))*@\\w+(\\.\\w{2,3}){1,3}$", message = "邮箱格式错误")
    @Column(name = "email", nullable = false, length = 200)
    private String email = "";

    @Column(name = "root", nullable = false)
    private Integer root = 0;

    @NotNull(message = "当前状态不能为空")
    @Min(value = 0, message = "当前状态'值'错误")
    @Max(value = 1, message = "当前状态'值'错误")
    @Column(name = "status", nullable = false)
    private Integer status;

    @Column(name = "login_ip", nullable = false, length = 64)
    private String loginIp = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Column(name = "login_time")
    private Date loginTime;

    @Column(name = "remark", nullable = false)
    private String remark = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    @Column(name = "create_by", nullable = false, length = 64)
    private String createBy = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    @Column(name = "update_by", nullable = false, length = 64)
    private String updateBy = "";

    @Min(value = 0, message = "所属部门'值'错误")
    @Column(name = "dept_id", nullable = false)
    private Long deptId = 0L;

    /* 部门名称 */
    @Transient
    private String deptName;
    /* 级联部门 */
    @Transient
    private List<Long> deptIds;
    /* 岗位信息 */
    @Transient
    private List<Long> postIds;
    /* 角色信息 */
    @Transient
    private List<Long> roleIds;

    public void edit(SysUser sysUser) {
        this.username = sysUser.username;
        this.mobile = sysUser.mobile;
        this.nickname = sysUser.nickname;
        this.work = sysUser.work;
        this.avatar = sysUser.avatar;
        this.sex = sysUser.sex;
        this.email = sysUser.email;
        this.status = sysUser.status;
        this.remark = sysUser.remark;
        this.deptId = sysUser.deptId;
    }
}
